import { loginApi } from '@/api/user'
const state = {
  token: localStorage.getItem('token699') || '',
  userInfo: {}
}
const mutations = {
  setToken(state, data) {
    // token
    localStorage.setItem('token699', data.data.jwt)
    state.token = data.data.jwt
    // 个人数据
    state.userInfo = data.data.user
    // console.log(data.data.user)
    localStorage.setItem('user', JSON.stringify(data.data.user))
  },
  removeToken(state) {
    // 清除vuex的token
    state.token = ''
  }
}
const actions = {
  async login(store, data) {
    // 登录请求
    const res = await loginApi(data)
    console.log('用户登录的信息', res)
    // 保存token
    store.commit('setToken', res)
  },
  logout(store) {
    // 清除本地存储的token
    localStorage.removeItem('token699')
    localStorage.removeItem('user')
    // 清除vuex的token
    store.commit('removeToken')
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
